require "proto/higharena/message/ReqHighArenaBattleMessage" -- 挑战请求
require "proto/higharena/message/ReqHighArenaSetDefFormationMessage" -- 设置防守队伍请求
require "proto/higharena/message/ReqHighArenaDrawArenaBoxMessage" -- 领取跨服竞技场宝箱请求
require "proto/higharena/message/ReqHighArenaBuyTicketMessage"   -- 购买挑战券请求
require "proto/higharena/message/ReqHighArenaOpponentMessage" -- 对手列表请求
require "proto/higharena/message/ReqHighArenaGetRanksMessage" -- 高阶竞技场排行请求
require "proto/higharena/message/ReqHighArenaBattleRecordsMessage" -- 高阶竞技场战斗记录请求
require "proto/higharena/message/ReqHighArenaSharedRecordsMessage" -- 高阶竞技场分享记录请求
require "proto/higharena/message/ReqHighArenaAddSharedRecordMessage" -- 高阶竞技场增加分享记录请求
require "proto/higharena/message/ReqHighArenaInfoMessage" -- 高阶竞技场详情请求
require "proto/higharena/message/ReqHighArenaGetAwardMessage" -- 请求领取挂机代币
require "proto/higharena/message/ReqHighArenaGetDanAwardMessage" -- 请求领取挂机代币

crossTeamArenaHandler = crossTeamArenaHandler or {}
-- 高阶竞技场详情返回
function crossTeamArenaHandler.receiveCrossTeamArenaInfo(msg) 
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossTeamBaseInfo(msg)
        FireEvent(Config.EventType.Fresh_Cross_Team_Arena)
        --if isOpenUI(PanelNames.UIAdventureMain) then
        --    local ui_adventure_main = CtrlManager.GetCtrl(PanelNames.UIAdventureMain)
        --    ui_adventure_main.RefreshArenaAward()
        --end
    end
end

--- 高阶竞技场挑战结果返回
function crossTeamArenaHandler.receiveCrossTeamBattleResult(msg)
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end
    logicMgr.ArenaManager.refreshCrossTeamBattleInfo(msg)
    -- FightManager.InitRewards(msg.items)
    -- BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)
    FightManager.cross_team_battle_data = {}
    BattlePlaybackManager.ClearMultiFightData()
    local win_count = 0
    if msg and msg.record then 
        local records = msg.record.records
        for k, v in ipairs(records) do
            if v.win == 1 then
                win_count = win_count + 1
            end
            FightManager.cross_team_battle_data[k] = v
            BattlePlaybackManager.AddMultiFightData( v.fightId,
                                                     v.win,
                                                     EnumConst.FightTypeConst.ARENA,
                                                     v,
                                                     nil,
                                                     -1,
                                                     -1)
        end
    end
    FightManager.cross_team_battle_data["defener_score"] = FightManager.arena_team_defener_score
    FightManager.cross_team_battle_data["attacker_score"] = FightManager.arena_team_attacker_score
    FightManager.cross_team_battle_data["win_count"] = win_count
    FightManager.arena_team_defener_score = nil
    FightManager.arena_team_attacker_score = nil
    -- BattlePlaybackManager.SetFormationType( EnumConst.FormationType.TeamCrossArenaAtk )
    BattlePlaybackManager.GotoNextFight(false)
end

-- 设置跨服组队防守队伍返回
function crossTeamArenaHandler.receiveCrossTeamDefFormation(msg) 
    if msg.state == 0 then 
        local my_def_formations = {}
        for k, v in ipairs(msg.defFormation) do
            my_def_formations[k] = v.formation
        end 
        logicMgr.FormationManager.SetFormation(EnumConst.FormationType.TeamCrossArenaDef, my_def_formations)
        FireEvent(Config.EventType.Fresh_CrossTeamArenaFormation)
    end
end

-- 领取高阶竞技场宝箱返回
function crossTeamArenaHandler.receiveCrossTeamDrawArenaBox(msg) 
    if msg.state == 0 then
        logicMgr.ArenaManager.addCrossTeamRewardBox(msg.boxId)
        local rewardsCfg = decodeJsonStrToTabel(G.dataTable["t_arenaboxrewardcs"][tostring(msg.boxId)].f_rewards)
        local rewards = {}
        for k, v in pairs(rewardsCfg) do
            local item = {};
            item.num = v[2];
            item.itemModelId = v[1]
            table.insert(rewards, item)
        end
        showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards)
        FireEvent(Config.EventType.Fresh_Cross_Team_Arena)
    end
end

-- 购买跨服组队竞技场挑战券返回
function crossTeamArenaHandler.receiveCrossTeamBuyTicket(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.addBuyCrossTeamTicketCount(msg.num)
        FireEvent(Config.EventType.Fresh_CrossTeamArenaTicket)
    end
end

--- 跨服组队竞技场对手列表返回
function crossTeamArenaHandler.receiveCrossTeamOpponentInfo(msg)
    if msg.state == 0 then
        ArenaManager.setCrossTeamOpponentInfo(msg)
        FireEvent(Config.EventType.Fresh_Cross_Team_Arena)
    end
end

--- 跨服组队竞技场战斗记录请求返回
function crossTeamArenaHandler.receiveCrossTeamArenaBattleRecords(msg)
    if msg.state == 0 then
        local records =  msg.records
        for k, v in ipairs(msg.serverIds) do
            if records[k] ~= nil then
                records[k].serverId = v
            end
        end
        logicMgr.ArenaManager.setCrossTeamRecordInfo(msg.recordVersion, records)
        FireEvent(Config.EventType.Fresh_CrossTeamArenaRecords)
    end
end

--- 跨服竞组队技场增加分享记录请求返回
function crossTeamArenaHandler.receiveCrossTeamAddSharedRecord(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.onAddCrossTeamSharedSuccess(msg.fightId)
    end
end
 
--- 跨服竞技场分享记录请求返回
function crossTeamArenaHandler.receiveCrossTeamArenaSharedRecords(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossTeamSharedRecords(msg)
        FireEvent(Config.EventType.Fresh_CrossTeamArenaSharedRecords)
    end
end

--  高阶竞技场排行返回
function crossTeamArenaHandler.receiveCrossTeamArenaGetRanks(msg)
    if msg.state == 0 then
        logicMgr.ArenaManager.setCrossTeamRankInfo(msg)
        FireEvent(Config.EventType.Fresh_CrossTeamArenaRanks)
    end
end

--  返回领取挂机代币
function crossTeamArenaHandler.receiveCrossTeamGetAward(msg)
    if msg.state == 0 and msg.award ~= nil then 
         logicMgr.ArenaManager.setCrossTeamAward(tonumber(tostring(msg.award)))
         FireEvent(Config.EventType.Fresh_CrossTeamArenaAward)
    end
end

--  新的段位奖励推送
function crossTeamArenaHandler.receiveHighArenaNewDanAwardPush(msg)
    if msg.state == 0 and msg.danId ~= nil then  
        logicMgr.ArenaManager.refreshCrossTeamNewDuanWeiAward( msg.danId, 1 )
        FireEvent(Config.EventType.Fresh_CrossTeamArenaDuanWeiAward)
    end
end

--  返回领取段位奖励
function crossTeamArenaHandler.receiveHighArenaGetDanAward(msg)
    if msg.state == 0 and msg.danId ~= nil then 
        local list = {}
        table.insert(list, msg.danId)
        logicMgr.ArenaManager.refreshCrossTeamNewDuanWeiAward( list, 2 )
        FireEvent(Config.EventType.Fresh_CrossTeamArenaDuanWeiAward)
    end
end

-------------------------------------------------------------------
-- 协议请求
-------------------------------------------------------------------

-- 挑战请求
function crossTeamArenaHandler.sendReqHighArenaBattle(foeId, isAi, formations, fightId) -- foeId 敌人ID isAi 是否是机器人 fightId 战斗id(有值代表为反击) formation 阵容
   local req = ReqHighArenaBattleMessage.New()
    req.foeId = foeId
    req.isAi = isAi
    local my_formations = {}
    for _, v in ipairs(formations) do
        local bean = HighArenaFormationInfo.New()
        bean.formation = v
        table.insert(my_formations, bean)
    end
    req.formation = my_formations
    req.version = Config.ProtoVersion -- 当前版本号
    if fightId ~= nil then
        req.fightId = fightId
    else
        req.fightId = 0
    end
    sendReqMessage(req)
end

-- 设置防守队伍请求
function crossTeamArenaHandler.sendReqHighArenaSetDefFormation(formation)
    local req = ReqHighArenaSetDefFormationMessage.New()
    local my_def_formation = {}
    for k, v in ipairs(formation) do
        local bean = HighArenaFormationInfo.New()
        bean.formation = v
        my_def_formation[k] = bean
    end
    req.defFormation = my_def_formation
    sendReqMessage(req)
end

-- 领取高阶竞技场宝箱请求
function crossTeamArenaHandler.sendReqHighArenaDrawArenaBox( boxId ) -- 宝箱Id
    local req = ReqHighArenaDrawArenaBoxMessage.New()
    req.boxId = boxId   -- 宝箱Id
    sendReqMessage(req)
end

-- 购买挑战券请求
function crossTeamArenaHandler.sendReqHighArenaBuyTicket( count )
    local req = ReqHighArenaBuyTicketMessage.New()
    req.num = count
    sendReqMessage(req)
end

--- 发送跨服挑战列表请求
function crossTeamArenaHandler.sendReqHighArenaOpponentMessage()
    local req = ReqHighArenaOpponentMessage.New()
    sendReqMessage(req)
end

--- 高阶竞技场排行请求
--- @param rankVersion number 排行榜版本号
function crossTeamArenaHandler.sendReqHighArenaGetRanks( rankVersion )
    local req = ReqHighArenaGetRanksMessage.New()
    req.rankVersion = rankVersion -- 高阶竞技场排行版本号
    sendReqMessage(req)
end

--- 高阶竞技场战斗记录请求 
function crossTeamArenaHandler.sendReqHighArenaBattleRecords(localRecordVer)
    local req = ReqHighArenaBattleRecordsMessage.New()
    req.recordVersion = localRecordVer
    sendReqMessage(req)
end

--- 高阶竞技场分享记录请求
function crossTeamArenaHandler.sendReqHighArenaSharedRecords()
    local req = ReqHighArenaSharedRecords.New()
    sendReqMessage(req)
end

--高阶竞技场增加分享记录请求
function crossTeamArenaHandler.sendReqHighArenaAddSharedRecord( _fight_id )
    local req = ReqHighArenaAddSharedRecordMessage.New()
    req.fightId = _fight_id
    sendReqMessage(req)
end

--高阶竞技场详情请求
function crossTeamArenaHandler.sendReqHighArenaInfo()
    local req = ReqHighArenaInfoMessage.New()
    sendReqMessage(req)
end

-- 请求领取挂机代币
function crossTeamArenaHandler.sendReqHighArenaGetAward()
    local req = ReqHighArenaGetAwardMessage.New()
    sendReqMessage(req)
end

-- 请求领取段位奖励
function crossTeamArenaHandler.sendReqHighArenaGetDanAward( _dan_id )
    local req = ReqHighArenaGetDanAwardMessage.New()
    req.danId = _dan_id
    sendReqMessage(req);
end
 